refundedController = BaseController.extend({
	template: 'refunded',
	center: '退款列表',
	mainView: 'refunded',
	headerTransparent: false,
	waitOn: function () {
		let self = this;
		self.limit = 30;
		self.state = {};
		Tracker.autorun(function(){
			self.merchantInfo = self.currentUser();
			if (self.merchantInfo) {
				self.refund_selector = { merchant_id: parseInt(self.merchantInfo.id) };
				self.subscribeHandleDict = {};
				self.subscribeHandle = self.getRefund;
		
				Meteor.subscribe('orders',{merchant_id: parseInt(self.merchantInfo.id)},{});
				return self.subscribeHandleDict['refunds'] = Meteor.subscribe('refunds', self.refund_selector, { sort: { id: -1 }, limit: self.limit }, self.subscribeCallbacks());
			}
		})
	},
	onRendered: function () {
		let self = this;
		var myApp = new Framework7({
			modalTitle: "提示",
			modalButtonOk: "确定",
			modalButtonCancel: "取消",
		});

		self.selected(0);
		self.paging({ selector: self.refund_selector, sorts: self.sorts, perpages: 4 }, function (options) {
			self.scrolling = true;
			self.limit += options.perpages;
			let subscribe = self.subscribeHandleDict['refunds'];
			if (subscribe) {
				subscribe.stop();
			}
		});
	},
	getRefund: function () {
		this.scrolling = false;
		return this.subscribeHandleDict['refunds'] = Meteor.subscribe('refunds', this.refund_selector, { sort: { id: -1 }, limit: this.limit }, this.subscribeCallbacks());
	},
	events: function (controller) {
		let self = this;
		return {
			'click #all': function (e) {
				self.selected(0);
				self.lastIndex = self.limit;
				self.refund_selector = { merchant_id: parseInt(self.merchantInfo.id) };
				self.subscribeHandle = self.getRefund;
				let subscribe = self.subscribeHandleDict['refunds'];
				if (subscribe) {
					subscribe.stop();
				}
			},
			'click #undo': function (e) {
				self.selected(1);
				self.lastIndex = self.limit;
				self.refund_selector.state = 'apply';
				self.subscribeHandle = self.getRefund;
				let subscribe = self.subscribeHandleDict['refunds'];
				if (subscribe) {
					subscribe.stop();
				}
			},
			'click #refunded': function (e) {
				self.selected(2);
				self.lastIndex = self.limit;
				self.refund_selector.state = 'done';
				self.subscribeHandle = self.getRefund;
				let subscribe = self.subscribeHandleDict['refunds'];
				if (subscribe) {
					subscribe.stop();
				}
			},
			'click #refuse': function (e) {
				self.selected(3);
				self.lastIndex = self.limit;
				self.refund_selector.state = 'refuse';
				self.subscribeHandle = self.getRefund;
				let subscribe = self.subscribeHandleDict['refunds'];
				if (subscribe) {
					subscribe.stop();
				}
			},
			'click .refuse': function (e) {
				let id = e.target.id;
				if(!id){
					return false;
				}
				let refund_id = parseInt(id);
				if (refund_id) {
					$('#'+id).attr('disabled', 'disabled');
					$('#'+refund_id+'agree').attr('disabled', 'disabled');
					$('#'+id).text('提交中...');
					self.post('merchant/refund/'+ refund_id, {state: 'refuse'}).then(function (res) {
						if (res.data && ('OK' == res.data.state)) {
							//$('#'+id).hide();
						}
					}, function (errer) {
						console.log(errer)
						$('#'+id).removeAttr('disabled');
						$('#'+refund_id+'agree').removeAttr('disabled');
                        $('#'+id).text('拒绝');
					})
				}
			},
			'click .agree': function (e) {
				let id = e.target.id;
				if(!id){
					return false;
				}
				let refund_id = parseInt(id);
				if (refund_id) {
					$('#'+id).attr('disabled', 'disabled');
					$('#'+refund_id+'refuse').attr('disabled', 'disabled');
					$('#'+id).text('提交中...');
					self.post('merchant/refund/'+ refund_id, {state: 'refunding'}).then(function (res) {
						if (res.data && ('OK' == res.data.state)) {
							//$('#'+id).hide();
						}
					}, function (errer) {
						console.log(errer)
						$('#'+id).removeAttr('disabled');
						$('#'+refund_id+'refuse').removeAttr('disabled');
                        $('#'+id).text('同意');
					})
				}
			},
		};
	},
	data: function () {
		return {
			scroll: 'infinite-scroll',
			refundList: function () {
				let refunds = Refunds.find({}, {sort: { id: -1 }}).fetch();
				if(refunds.length>0){
					for(var i in refunds){
						if(refunds[i].order_id){
							refunds[i].order = Orders.findOne({id: parseInt(refunds[i].order_id)},{});
						}
					}
				}
                return refunds;
			},
		}
	},
	selected: function (i) {
		$(".x7-refunded-select div").removeClass('active').eq(i).addClass('active');
	},
})
